<?php

    $dbfoto = $this->database->t_foto;
    if($page=="recent") {
        $query = "SELECT * FROM ".$dbfoto."
        ORDER BY ".$dbfoto.".date_upload DESC";
    } else if($page=="popular") {
        $dbcommentfoto = $this->database->t_comment_foto;
        $query = "SELECT ".$dbfoto.".id, ".$dbfoto.".username_uploader, ".$dbfoto.".img_src, ".$dbfoto.".id_role, ".$dbfoto.".title, count(".$dbcommentfoto.".id_foto) AS comment_counter
        FROM ".$dbfoto." LEFT JOIN ".$dbcommentfoto."
        ON ".$dbfoto.".id = ".$dbcommentfoto.".id_foto
        GROUP BY ".$dbfoto.".id
        ORDER BY count(".$dbcommentfoto.".id_foto) DESC";
    } else if($page=="rating") {
        $dbratefoto = $this->database->t_fotomark;
        $query = "SELECT ".$dbfoto.".id, ".$dbfoto.".username_uploader, ".$dbfoto.".img_src, ".$dbfoto.".id_role, ".$dbfoto.".title, sum(".$dbratefoto.".rate) AS rate_counter
        FROM ".$dbfoto." LEFT JOIN ".$dbratefoto."
        ON ".$dbfoto.".id = ".$dbratefoto.".id_foto
        GROUP BY ".$dbfoto.".id
        ORDER BY sum(".$dbratefoto.".rate) DESC";
    } else {
        $query = "SELECT * FROM ".$dbfoto."
        ORDER BY ".$dbfoto.".date_upload DESC";
    }

    $result = $this->database->execQuery($query);
    $logged = $this->session->isLoggedIn;
    $count = mysql_num_rows($result);
    for($i=1;$i<$count+1;++$i) {
        $data=mysql_fetch_array($result);
        $all_allowed = true;
        $logged_allowed = false;
        $follower_allowed = false;
        $private_allowed = false;
        if($data['id_role']==1) {
            $all_allowed = true;
        } else if($data['id_role']==2) {
            $logged_allowed=$logged;
        } else if($data['id_role']==3){
//            if($logged) {
//                $dbfollow = $this->database->t_follow;
//                $query_follow = "SELECT * FROM $dbfollow
//                    WHERE $dbfollow.username='".$this->session->username."' AND $dbfollow.follow = '".$data['username_uploader']."'";
//                $result_follow = $this->database->execQuery($query_follow);
//                if(mysql_num_rows($result_follow)>0) {
//                    $follower_allowed=true;
//                }
//            }
        } else if($data['id_role']==4) {
//            if($logged) {
//                if($this->session->username==$data['username_uploader']) {
//                    $private_allowed=true;
//                }
//            }
        }
        if($all_allowed || $logged_allowed || $follower_allowed || $private_allowed) {
        $img_src_here = "res/upload/".substr($data['img_src'], strrpos($data['img_src'], '/')+1);
        $img_ext = substr($data['img_src'], strrpos($data['img_src'], '.'));
        $photo_id = $data['id'].$img_ext;
        $id=$data['username_uploader'];
        if($page=="recent") {
            $title = $data['title'];
        } else if($page=="popular") {
            $title = $data['title']." (".$data['comment_counter'].")";
        } else if($page=="rating") {
            $title = $data['title']." (".$data['rate_counter'].")";
        } else {
            $title = $data['title'];
        }
        $link_profile = "profile.php?id=".$id;

        $dbuser = $this->database->t_user;
        $queryuser = "SELECT fullname FROM ".$dbuser."
                    WHERE username='$id'";
        $resultuser = $this->database->execQuery($queryuser);
        $datauser=mysql_fetch_array($resultuser, MYSQL_ASSOC);
        $source_profile = $datauser['fullname'];
?>
    <div class="image_wrap">
        <a href="profile.php?page=photo-detail&id=<?php echo $id ?>&photo-id=<?php echo $photo_id ?>">
            <div class="top"></div>
            <div class="left"></div>
            <div class="image_thumbnail">
                <img alt="contoh" src="<?php echo $img_src_here ?>" />
            </div>
            <div class="right"></div>
            <div class="clear_both"></div>
            <div class="bottom"></div>
        </a>
         <div class="title">
           <?php echo $title?>
        </div>
        <div class="owner">
            By <a href="<?php echo $link_profile; ?>"><?php echo $source_profile; ?></a>
        </div>
    </div>
    <?php
        } else {
            --$i;
        }
    if($i%4==0) {
    ?>
    <div class="clear_both"></div>
<?php }
        if($i==12) {
            break;
        }
    }
?>